'use strict';

/**
 * Config for the router
 */
angular.module('app')
  .run(
    [          '$rootScope', '$state', '$stateParams',
      function ($rootScope,   $state,   $stateParams) {
          $rootScope.$state = $state;  
          $rootScope.$stateParams = $stateParams;        
      }
    ]
  )
  .config(
    [          '$stateProvider', '$urlRouterProvider', 'JQ_CONFIG', 'MODULE_CONFIG', 
      function ($stateProvider,   $urlRouterProvider,  JQ_CONFIG, MODULE_CONFIG) {
          var layout = "tpl/app.html";
          

        $urlRouterProvider
            .otherwise('/access/signin');
          
          $stateProvider
              .state('app', {
                  abstract: true,
                  url: '/app',
                  templateUrl: layout
              })
              .state('app.dashboard', {
                  url: '/dashboard',                  
                  templateUrl: 'tpl/app_dashboard.html',
                  resolve: load([
                          // 'js/controllers/dashboard/china.js',
                          // 'js/controllers/dashboard/echarts.js',
                          'js/controllers/dashboard/dashboard.js'                         
                          
                        ])
              })
         
              //monitor
              .state('app.monitor', {
                  url: '/monitor',
                  template: '<div ui-view class="fade-in-up"></div>'
              })
              .state('app.monitor.electricquantity', {
                  url: '/electricquantity/{page}',
                  templateUrl: 'tpl/monitor/electricquantity.html',
                  resolve: load([                       
                       'js/controllers/monitor/electric/monitorele.js'
                      ])
              })
              .state('app.monitor.selectricdetail',{
                  url: '/selectricdetail/[{roomid}]',
                  templateUrl: 'tpl/monitor/electric/selectricdetail.html',
                  resolve: load([                       
                       'js/controllers/monitor/electric/selectricdetail.js'
                  ])
              })
              .state('app.monitor.topdata',{
                  url: '/topdata/[{layerdata},{key}]',
                  templateUrl: 'tpl/monitor/electric/top.html',
                  resolve: load([                       
                       'js/controllers/monitor/electric/top.js'
                  ])
              })
              .state('app.monitor.environment', {
                  url: '/environment/{page}',
                  templateUrl: 'tpl/monitor/environment.html',
                  resolve: load([                       
                       'js/controllers/monitor/environment/monitorenv.js'
                  ])
              })

              .state('app.monitor.envparadtail',{
                url:'/envparadtail/[{did},{page}]',
                templateUrl:'tpl/monitor/environment/envparadtail.html',
                resolve: load([ 
                       'css/font_qxz/iconfont.css',                      
                       'css/envdetail.css'
                      ])
              }) 
              .state('app.monitor.GIS', {
                  url: '/GIS',
                  templateUrl: 'tpl/monitor/GIS.html'
              })
       
              //控制管理
              .state('app.control', {
                  url: '/control',
                  template: '<div ui-view></div>'
              })
              .state('app.control.electricquantity', {
                  url: '/electricquantity',
                  templateUrl: 'tpl/control/electricquantity.html',
                  resolve: load([                       
                       'js/controllers/control/electric/monitorele.js'
                      ])
              })

              .state('app.control.selectricdetail', {
                  url: '/selectricdetail/{roomid}',
                  templateUrl: 'tpl/control/electric/selectricdetail.html',
                  resolve: load([                       
                       'js/controllers/control/electric/selectricdetail.js',
                       'js/controllers/control/electric/modal.js'
                      ])
              })
              .state('app.control.paraset', {
                  url: '/paraset/{did}',
                  templateUrl: 'tpl/control/electric/paraset.html',
                  resolve: load([                       
                       'js/controllers/control/electric/manyparamsset.js'                       
                      ])
              })
              .state('app.control.ydtail',{
                url:'/ydtail/[{did},{page}]',
                templateUrl:'tpl/control/detail/ydtail.html',
                resolve: load([ 
                       'css/font_qxz/iconfont.css',                      
                       'css/envdetail.css'
                      ])
              }) 
            
              .state('app.control.air-condition', {
                  url: '/air-condition',
                  templateUrl: 'tpl/control/air-condition.html'
              })
              .state('app.control.public', {
                  url: '/public',
                  templateUrl: 'tpl/control/public.html'
              })

              //stat
              .state('app.stat', {
                  url: '/stat',
                  template: '<div ui-view class="fade-in"></div>'
                  
              })
              .state('app.stat.statdata', {
                  url: '/statdata',
                  templateUrl: 'tpl/stat/statdata.html'                 
              })

              .state('app.stat.breakrules', {
                  url: '/breakrules',
                  templateUrl: 'tpl/stat/breakrules.html'                 
              })
              .state('app.stat.fault', {
                  url: '/fault',
                  templateUrl: 'tpl/stat/fault.html'                 
              })
              .state('app.stat.environment', {
                  url: '/environment',
                  templateUrl: 'tpl/stat/environment.html'                 
              })
              .state('app.stat.month', {
                  url: '/month',
                  templateUrl: 'tpl/stat/month.html'                 
              })

              //order
               .state('app.order', {
                  url: '/order',
                  template: '<div ui-view></div>'
              })

              .state('app.order.breakrules',{
                url:'/breakrules',
                templateUrl:'tpl/order/breakrules.html',
                resolve: load([                 
                  'js/controllers/order/breakrules/breakrules.js',
                  'js/controllers/order/breakrules/modal.js'
                ])
              })

              .state('app.order.dobreakrules',{
                url:'/dobreakrules/{doid}',
                templateUrl:'tpl/order/breakrules/dobreakrules.html',
                resolve: load([
                  'css/iconfont/iconfont.css',
                  'css/timeline.css',
                  'js/controllers/order/breakrules/dobreakrules.js'

                ])
              })

              .state('app.order.breakrulesdetail',{
                url:'/breakrulesdetail/{oid}',
                templateUrl:'tpl/order/breakrules/breakrulesdetail.html',
                resolve: load([
                  'js/controllers/order/breakrules/detailbreakrules.js'

                ])
              })
              .state('app.order.fault',{
                url:'/fault/{page}',
                templateUrl:'tpl/order/fault.html',
                resolve: load([
                  'css/iconfont/iconfont.css',
                  'js/controllers/order/fault/modal.js',
                  'js/controllers/order/fault/fault.js'
                ])
              })

             .state('app.order.newfault',{
                url:'/newfault',
                templateUrl:'tpl/order/fault/newfault.html',
                resolve: load([
                  'css/iconfont/iconfont.css',
                  'css/timeline.css',                
                  'js/controllers/order/fault/newfault.js'
                ])
              })

              .state('app.order.jiejing',{
                url:'/jiejing/{oid}',
                templateUrl:'tpl/order/fault/jiejing.html',
                resolve: load([
                  'css/iconfont/iconfont.css',
                  'css/timeline.css',                   
                  'js/controllers/order/fault/jiejing.js'
                ])
              })

              .state('app.order.wangong',{
                url:'/wangong/{oid}',
                templateUrl:'tpl/order/fault/wangong.html',
                resolve: load([
                  'css/iconfont/iconfont.css',
                  'css/timeline.css',                   
                  'js/controllers/order/fault/wangong.js'
                ])
              })

              .state('app.order.queren',{
                url:'/queren/{oid}',
                templateUrl:'tpl/order/fault/queren.html',
                resolve: load([
                  'css/iconfont/iconfont.css',
                  'css/timeline.css',                   
                  'js/controllers/order/fault/queren.js'
                ])
              })

              .state('app.order.chexiao',{
                url:'/chexiao/{oid}',
                templateUrl:'tpl/order/fault/chexiao.html',
                resolve: load([
                  'css/iconfont/iconfont.css',
                  'css/timeline.css',                   
                  'js/controllers/order/fault/chexiao.js'
                ])
              })


              .state('app.order.faultdetail',{
                url:'/faultdetail/{oid}',
                templateUrl:'tpl/order/fault/faultdetail.html',
                resolve:load([
                  'css/iconfont/iconfont.css',
                  'css/timeline.css',
                  'js/controllers/order/fault/faultdetail.js'

                ])
              })

              //user
              .state('app.user', {
                  url: '/user',
                  template: '<div ui-view class="fade-in-up"></div>'
              })
              .state('app.user.info', {
                  url: '/info',
                  templateUrl: 'tpl/user/info.html',
                  resolve:load(['js/controllers/user/userinfo.js'])                  
              })

              .state('app.user.modifyinfo', {
                  url: '/modifyinfo',
                  templateUrl: 'tpl/user/modifyinfo.html',
                  resolve:load([
                    'js/controllers/user/modifyinfo.js',
                    'js/uploadify/uploadify.css',
                    'js/uploadify/jquery.uploadify.min.js'
                    ])                  
              })

              .state('app.user.modifypassword', {
                  url: '/modifypassword',
                  templateUrl: 'tpl/user/modifypassword.html' ,
                  resolve:load(['js/controllers/user/modifypwd.js'])                 
              })
            
              //sys
               .state('app.sys', {
                  url: '/sys',
                  template: '<div ui-view class="fade-in-down"></div>'
              })
              .state('app.sys.campus', {
                  url: '/campus/{page}',
                  templateUrl: 'tpl/sys/campus.html',                 
                  resolve: load([
                    'js/controllers/sys/campus/modal.js',
                    'js/controllers/addrs.js',
                    'js/controllers/sys/campus/campus.js'
                  ])
              })
              .state('app.sys.campusconfig',{
                  url: '/campusconfig/[{campusid},{page}]',
                  templateUrl:'tpl/sys/campus/campusconfig.html',
                  resolve: load([
                    'js/controllers/sys/campus/campusconfig.js',
                    'js/controllers/sys/campus/building/modal.js'
                    
                  ])
              }) 

              .state('app.sys.campusconfigedit',{
                  url: '/campusconfigedit/[{cbid},{cmid}]',
                  templateUrl:'tpl/sys/campus/campusconfigedit.html',
                  resolve: load([
                    'css/campusbulidingedit.css',
                    'js/controllers/sys/campus/campusconfigedit.js',
                    
                  ])
              }) 
              .state('app.sys.product', {
                  url: '/product/{ppage}',
                  templateUrl: 'tpl/sys/product.html',
                  resolve: load([
                    'js/controllers/sys/product/modal.js',                    
                    'js/controllers/sys/product/product.js'
                  ])
              })
              .state('app.sys.devicelist',{
                  url: '/devicelist/[{pid},{pname},{dpage}]',
                  templateUrl:'tpl/sys/product/device/devicelist.html',
                  resolve: load([
                    'js/controllers/sys/product/device/modal.js',
                    'js/controllers/addrs.js',
                    'js/controllers/sys/product/device/device.js'
                  ])

              })

              .state('app.sys.editdev',{
                  url: '/editdev/[{devid},{epage},{pid}]',
                  templateUrl:'tpl/sys/product/device/editdev.html',
                  resolve: load([                   
                    'js/controllers/addrs.js',
                    'js/controllers/sys/product/device/editdev.js'
                  ])

              })

              .state('app.sys.editdevbasicinfo',{
                  url: '/editdevbasicinfo/[{devid},{ebpage}]',
                  templateUrl:'tpl/sys/product/device/editdevbasicinfo.html',
                  resolve: load([ 
                    'js/controllers/sys/product/device/editdev.js'
                  ])

              })
              .state('app.sys.editdevadditionalinfo',{
                  url: '/editdevadditionalinfo/[{devid},{eadpage}]',
                  templateUrl:'tpl/sys/product/device/editdevadditionalinfo.html',
                  resolve: load([ 
                    'js/controllers/sys/product/device/editdev.js'
                  ])

              })

               .state('app.sys.assignroom',{
                  url: '/assignroom/[{devid},{anpage},{pid}]',
                  templateUrl:'tpl/sys/product/device/assignroom.html',
                  resolve: load([         
                   
                    'js/controllers/sys/product/device/assignroom.js'
                  ])

              })
              .state('app.sys.role', {
                  url: '/role/{page}',
                  templateUrl: 'tpl/sys/role.html',
                  resolve: load([
                    'js/controllers/sys/role/modal.js',                   
                    'js/controllers/sys/role/role.js'
                  ])

              })

              .state('app.sys.person', {
                  url: '/person/{page}',
                  templateUrl: 'tpl/sys/person.html',
                  resolve: load([
                    'js/controllers/sys/person/modal.js',                   
                    'js/controllers/sys/person/person.js'
                  ])

              })

              .state('app.sys.unit', {
                  url: '/unit/{page}',
                  templateUrl: 'tpl/sys/unit.html',
                  resolve: load([
                    'js/controllers/sys/unit/modal.js',                   
                    'js/controllers/sys/unit/unit.js'
                  ])

              })

              .state('app.sys.unitinfo', {
                  url: '/unitinfo/[{unitid},{page}]',
                  templateUrl: 'tpl/sys/unit/unitinfo.html',
                  resolve: load([                                    
                    'js/controllers/sys/unit/unitinfo.js'
                  ])

              })

              .state('app.sys.roleauth', {
                  url: '/roleauth/[{page},{roleid}]',
                  templateUrl: 'tpl/sys/role/roleauth.html',
                  resolve: load([
                    '//kendo.cdn.telerik.com/2016.3.914/styles/kendo.common-material.min.css',
                    '//kendo.cdn.telerik.com/2016.3.914/styles/kendo.material.min.css',
                    '//kendo.cdn.telerik.com/2016.3.914/styles/kendo.material.mobile.min.css',
                    'js/kendo.all.min.js',
                    'js/controllers/sys/role/roleauth.js'                 
                    
                  ])

              })
              .state('app.sys.user', {
                  url: '/user/{page}',
                  templateUrl: 'tpl/sys/user.html',
                  resolve: load([
                    'js/uploadify/uploadify.css',
                    'js/uploadify/jquery.uploadify.min.js',
                    'js/controllers/sys/user/modal.js',                   
                    'js/controllers/sys/user/user.js'
                  ])


              })

              .state('app.sys.usersignauth', {
                  url: '/usersignauth/[{userid},{page}]',
                  templateUrl: 'tpl/sys/user/usersignauth.html',
                  resolve: load([                                   
                    'js/controllers/sys/user/signauth.js'
                  ])
              })

              // others
        
              .state('access', {
                  url: '/access',
                  template: '<div ui-view class="fade-in-right-big smooth"></div>'
              })
              .state('access.signin', {
                  url: '/signin',
                  templateUrl: 'tpl/page_signin.html',              
                  // resolve: load( ['css/login.css'] )
              })
             .state('access.monitortopdata',{
                  url: '/monitortopdata/[{layerdata},{key}]',
                  templateUrl: 'tpl/monitor/electric/top.html',
                  resolve: load([                       
                       'js/controllers/monitor/electric/top.js'
                  ])
              })           

             

          function load(srcs, callback) {
           // console.log(srcs);
            return {
                deps: ['$ocLazyLoad', '$q',
                  function( $ocLazyLoad, $q ){
                    var deferred = $q.defer();
                    var promise  = false;
                    srcs = angular.isArray(srcs) ? srcs : srcs.split(/\s+/);
                    if(!promise){
                      promise = deferred.promise;
                    }
                    angular.forEach(srcs, function(src) {
                      promise = promise.then( function(){
                        if(JQ_CONFIG[src]){
                          return $ocLazyLoad.load(JQ_CONFIG[src]);
                        }
                        angular.forEach(MODULE_CONFIG, function(module) {
                          if( module.name == src){
                            //console.log(src);
                            name = module.name;
                          }else{
                            name = src;
                          }
                        });
                        //console.log(name);
                        //console.log($ocLazyLoad.load(name))
                        return $ocLazyLoad.load(name);

                      } );
                    });
                    deferred.resolve();
                    return callback ? promise.then(function(){ return callback(); }) : promise;
                }]
            }
          }


      }
    ]
  );
